<!--头部公共部分-->
{include file="common/header" /}
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a><cite>课程:{$course_name}</cite></a>
        <a><cite>上课进度</cite></a>
      </span>
</div>
<input type="hidden" id="course_id" value="{$course_id}">
<input type="hidden" id="course_name" value="{$course_name}">
<div id="wodekehu" class="x-body" class="clear:both;">
    <table class="layui-table" id="datalist" lay-filter="Event"></table>
</div>
<div class="layui-form-item">
    <div class="layui-input-block">
		<!--
        <input class="layui-btn" id="lists" value="下一步:课程授权" readonly>
		-->
        <a class="layui-btn " id="save">保存</a>
        <a class="layui-btn layui-btn-primary" id="fanhui">返回上级</a>
    </div>
</div>
<!--底部公共部分-->
{include file="common/footer" /}
<script>
    layui.use(['table','element','layer'], function () {
        let table = layui.table,
            layer = layui.layer,
            element = layui.element,
            $ = layui.$;

        let loading = layer.load();
        let course_id = $("#course_id").val();
        let course_name = $("#course_name").val();

        $('#fanhui').click(function () {
            location.href = "{:url('Course/edit')}?id=" + course_id;
        });

        $('#save').click(function () {
            location.href = "{:url('Course/lists')}";
        });

        $('#lists').click(function () {
            location.href = "{:url('Course/authorization')}?id=" + course_id + "&course_name="+ course_name;
        });

        //方法级渲染
        let tableIns = table.render({
            elem: '#datalist'
            , url: '{:url("Schedule/lists")}?course_id=' + course_id
            , method: 'POST'
            , toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code === 1) {
                    res.code = 0;
                } else {
                    res.code = -1;
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [[
                {type: 'checkbox'},
                {field: 'class_times',align: 'center', title: '课次'},
                {field: 'class_chapter',align: 'center',edit: 'text', title: '课章节'},
                {field: 'class_content',align: 'center',edit: 'text', title: '上课内容'},
            ]]
            , height: 'full-180'
            , page: true
            , done: function () {
                layer.close(loading);
            }
        });

        //监听头工具栏事件
        table.on('toolbar(Event)', function(obj){
            let checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    layer.open({
                        type: 2,
                        title: '新增上课进度',
                        shadeClose: true,
                        shade: 0.5,
                        area: ['70%', '75%'],
                        content: '{:url("Schedule/add")}?course_id=' + course_id
                    });
                    break;
                case 'update':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                        return;
                    } else if(data.length > 1){
                        layer.msg('只能同时编辑一个');
                        return;
                    } else {
                        let id = checkStatus.data[0].id;
                        layer.open({
                            type: 2,
                            title: '编辑上课进度',
                            shadeClose: true,
                            shade: 0.5,
                            area: ['70%', '75%'],
                            content: '{:url("Schedule/edit")}?id=' + id + '&course_id=' + course_id
                        });
                    }
                    break;
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请至少选择一行');
                        return;
                    }
                    let checked_ids = [];
                    let file_id= 0;
                    for (let index = 0; index < data.length; index++) {
                        const element = data[index];
                        checked_ids.push(element.id)
                        file_id = element.file_id;
                    }
                    let ids ={"ids":checked_ids.join(',')};
                    console.log(ids);
                    let url = '{:url("Schedule/del")}';
                    $.post(url,ids,function(response){
                        console.log(response);
                        if(response.code === 1){
                            tableIns.reload();
                        }else{
                            layer.msg(response.msg);
                        }
                    });
                    break;
            };
        });
    });
</script>
